package com.slmxz.tms.biz.controller;

import com.slmxz.tms.biz.model.entity.Dispatch;
import com.slmxz.tms.biz.model.vo.DispatchVO;
import com.slmxz.tms.biz.service.DispatchService;
import com.slmxz.tms.common.support.Condition;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import com.slmxz.tms.common.model.Query;

/**
 * 调度计划前端控制层
 *
 * @author sunyao
 *@Date 2025-04-02 21:26
 */
@Tag(name = "调度计划接口")
@RestController
@RequestMapping("/api/slmz/tms/dispatch")
@RequiredArgsConstructor
public class DispatchController  {

    private final DispatchService dispatchService;

    @Operation(summary = "调度计划分页列表")
    @GetMapping("/page")
    public PageResult<DispatchVO> selectDispatchPage(Query query, DispatchVO dispatch) {
        IPage<DispatchVO> result = this.dispatchService
                .selectDispatchPage(Condition.getPage(query),dispatch);
        return PageResult.success(result);
    }

    @Operation(summary = "新增调度计划")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('biz:dispatch:add')")
    public Result<Void> saveDispatch(@RequestBody @Valid DispatchVO  dispatch) {
        boolean result = this.dispatchService.saveDispatch(dispatch);
        return Result.result(result);
    }

    @Operation(summary = "获取调度计划详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('biz:dispatch:edit')")
    public Result<Dispatch> selectDispatchDetail(
        @Parameter(description = "调度计划ID") @PathVariable Long id
    ) {
        Dispatch dispatch = this.dispatchService.selectDispatchDetail(id);
        return Result.success(dispatch);
    }

    @Operation(summary = "修改调度计划")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('biz:dispatch:edit')")
    public Result<Void> updateDispatch(
            @RequestBody @Validated DispatchVO dispatch
    ) {
        boolean result = dispatchService.updateDispatch(dispatch);
        return Result.result(result);
    }

    @Operation(summary = "删除调度计划")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('biz:dispatch:delete')")
    public Result<Void> deleteDispatchs(
        @Parameter(description = "调度计划ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.dispatchService.deleteDispatchs(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销调度计划")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<Void> activeOrLogout(@RequestBody DispatchVO dispatch) {
        boolean result = this.dispatchService.activeOrLogout(dispatch);
        return Result.result(result);
    }
}
